$(document).ready(function () {
	
	//create float
	_form = $('form').html();
	$('form').html('<div class="form" style="float:left; width:60%;">'+ _form +'</div><div class="extra" style="float:left; width:38%;"></div><div style="clear:both"></div>');
	
	//add extra element to end
	$('.extra').html('<label><br/><span id="add-element"><img src="'+baseurl+'/img-pack/application/buttons/add-extra.png" width="20" align="absmiddle"/> '+ JAVASCRIPT_ADD_EXTRA_ELEMENT +'</span><br/><label>');
	$('span#add-element').css('cursor', 'pointer');
	
	//add generic element
	$('span#add-element').live('click', function(e) {
		
		//create element
		$last = $('span#add-element');
		
		//create content
		_genelement = '<label>'+ JAVASCRIPT_ADD_EXTRA +' - <input type="text" class="extrakey required" name="extra[key][]"/> - <span class="remove-extra"><img src="'+baseurl+'/img-pack/application/buttons/remove-extra.png" width="15"/></span></label>';
		_genelement += '<span><input type="text" class="extravalue textfield required" name="extra[value][]"/></span>';
		
		//insert element
		$(_genelement).insertAfter($last);
		$('span.remove-extra').css('cursor', 'pointer');
		return false;
	});
	
	//add mask to telephone
	$(".phone").mask('99.99.99.99.99');
	//add mask to zipcode
	$('.zipcode').mask('99999');

	
	$(".multiselect").multiselect({
		checkAllText:"CHECK ALL",
		uncheckAllText:"UNCHECK ALL",
		noneSelectedText:"",
		selectedText:"# SELECTED"
	});
	
	//create extra from extra tag
	$('extra').each(function () {
		$extra = $(this);
		//create element
		$last = $('span#add-element').parent();
		//create content
		_genelement = '<label>'+ JAVASCRIPT_EDIT_EXTRA +' - <input type="text" class="extrakey required" name="extra[key][]" value="'+$extra.attr('key')+'" /> - <span class="remove-extra"><img src="'+baseurl+'/img-pack/application/buttons/remove-extra.png" width="15"/></span></label>';
		_genelement += '<span><input type="text" class="extravalue textfield required" name="extra[value][]"  value="'+$extra.attr('value')+'"/></span>';
		//insert element
		$(_genelement).insertBefore($last);
		$('span.remove-extra').css('cursor', 'pointer');
		//remove extra from tag
		$extra.remove();
	});
	
	//remove generic extra
	//remove generic extra
	$('span.remove-extra').live('click', function(e) {
		
		$(this).parent().next().fadeOut(500, function () {
			$(this).remove();
		});
		
		$(this).parent().fadeOut(500, function () {
			$(this).remove();
		});
	});
	
	$(document).keypress(function (e) {
		key = (e.keyCode) ? e.keyCode : e.which;
		if (key === 13) {
			$('form').submit();
		}
	});
	
});

var process = function(data, target) {
	if (data.updated == false) {
		$('#documentresponse').html(data.message);
    }
    else {
    	//add message
		$('#structure-form').html('<p align="center">' + data.message + '</p>');
		$('#submitter').fadeOut(function () {
			$(this).remove();
			//create button
	    	jQuery('#cancel').val(JAVASCRIPT_CLOSE_WINDOW);
			$('#documentresponse').empty();
		});
	}
};